package com.lqk.demo.concurrencytools.threadpool.juc.countdownlatch;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

import static com.lqk.demo.n2.util.Sleeper.sleep;

/**
 * @author lqk
 * @date 2022/1/19 9:45
 *
 * 用来进行线程同步协作，等待所有线程完成倒计时。
 * 其中构造参数用来初始化等待计数值，await() 用来等待计数归零，countDown() 用来让计数减一
 */
@Slf4j
public class TestCountDownLatch {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);

        new Thread(()->{
            log.debug("begin...");
            sleep(1);
            latch.countDown();
            log.debug("end...{}",latch.getCount());
        }).start();

        new Thread(()->{
            log.debug("begin...");
            sleep(2);
            latch.countDown();
            log.debug("end...{}",latch.getCount());
        }).start();

        new Thread(()->{
            log.debug("begin...");
            sleep(1.5);
            latch.countDown();
            log.debug("end...{}",latch.getCount());
        }).start();

        log.debug("waiting...");
        latch.await();
        log.debug("wait end...");

    }
}
